<template>
	<view>
		<uni-forms ref="form" :modelValue="form" :rules="rules" :label-width="80" style="background-color: #fff;">
			<uni-forms-item label="道闸名称" required name="name">
				<uni-easyinput v-model="form.name"></uni-easyinput>
			</uni-forms-item>
			<uni-forms-item label="闸门类型" required name="gateType">
				<uni-data-select v-model="form.gateType" :localdata="typeOptions"></uni-data-select>
			</uni-forms-item>
			<view v-if="form.gateType =='hikvision'">
				<uni-forms-item label="道闸IP" required name="gateIp">
					<uni-easyinput v-model="form.gateIp"></uni-easyinput>
				</uni-forms-item>
				<uni-forms-item label="道闸端口" required name="gatePort">
					<uni-easyinput v-model="form.gatePort"></uni-easyinput>
				</uni-forms-item>
				<uni-forms-item label="道闸用户名" required name="gateUsername">
					<uni-easyinput v-model="form.gateUsername"></uni-easyinput>
				</uni-forms-item>
				<uni-forms-item label="道闸密码" required name="gatePassword">
					<uni-easyinput v-model="form.gatePassword"></uni-easyinput>
				</uni-forms-item>
			</view>
		</uni-forms>
	</view>
</template>

<script>
	import {
		getGate,
		addGate,
		updateGate
	} from "@/api/control/gate.js";
	import {
		getDicts
	} from "@/api/system/dict/data"
	export default {
		components: {},
		data() {
			return {
				form: {
					id: null,
					name: null,
					gateIp: null,
					gatePort: null,
					gateUsername: null,
					gatePassword: null,
					gateType: null,
					createBy: null,
					createTime: null,
					updateBy: null,
					updateTime: null,
					remark: null
				},
				rules: {

				},
				isEdit: false,
				typeOptions: []
			}
		},
		onLoad(options) {
			getDicts('gate_type').then(res => {
				this.typeOptions = res.data
				this.typeOptions.forEach(d => {
					d.value = d.dictValue
					d.text = d.dictLabel
				})
			})
			if (options.id > 0) this.queryById(options.id)
		},
		methods: {
			onNavigationBarButtonTap() {
				this.submitForm()
			},
			queryById(id) {
				getGate(id).then(res => {
					this.form = res.data
				})
			},
			submitForm() {
				this.$refs.form.validate(['id'], (err, form) => {
					if (!err) {
						if (this.form.id != null) {
							updateGate(this.form).then(response => {
								this.$modal.msgSuccess("修改成功");
								this.isEdit = false
							});
						} else {
							addGate(this.form).then(response => {
								this.$modal.msgSuccess("新增成功");
								this.isEdit = false
							});
						}
					}
				});
			},
		}
	}
</script>

<style scoped>
page {
		display: flex;
		flex-direction: column;
		box-sizing: border-box;
		background-color: #fff;
		min-height: 100%;
		height: auto;
	}
</style>